package com.wolf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/26 0026.
 */
@Controller
public class upImgUtil {
    @RequestMapping("upImg")
    @ResponseBody
    public Map<String,Object> test(String base64,String imags){
        Map<String,Object> map = new HashMap<String, Object>();
        String[] ww= base64.split(",");
        base64=ww[1];
        String[] aa = base64.split("\"");
        base64=aa[0];
        try{
            String path = "D:\\wolfs\\wolf_store\\src\\main\\resources\\templates\\img\\"+imags;
            map.put("data","success");
            upImgUtil.decoderBase64File(base64,path);
        }catch(Exception e){
            map.put("data","error");
            e.printStackTrace();
        }
        return map;
    }
    public static void decoderBase64File(String base64Code, String targetPath) {
        byte[] buffer;
        FileOutputStream out = null;
        try {
            buffer = new BASE64Decoder().decodeBuffer(base64Code);
            out = new FileOutputStream(targetPath);
            out.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
